import os

from absl import flags

FLAGS = flags.FLAGS


flags.DEFINE_string("key_root", "../../coin_key/view", "key_root")

flags.DEFINE_string('key_name', None, 'key_name')


def get_key_file_impl(key_root, key_name, market_type, exchange, owner, api_version):
  exchange_api_to_dir_map = {
      ('ExWallet', 'HwWallet', 'v1'): 'hw_wallet',
      ('ExWallet', 'Kucoin', 'v1'): 'kucoin',
      ('ExWallet', 'Okex', 'v5'): 'OKEx',
      ('Futures', 'Bitmex', 'v1'): 'bitmex',
      ('Futures', 'Binance', 'v1'): 'binance',
      ('Futures', 'Binance', 'v1-delivery'): 'binance',
      ('Futures', 'Bybit', 'v3'): 'bybit',
      ('Futures', 'Bybit', 'v3-linear'): 'bybit',
      ('Futures', 'Deribit', 'v2'): 'Deribit',
      ('Futures', 'Dydx', 'v3'): 'dydx',
      ('Futures', 'Ftx', 'v1'): 'FTX',
      ('Futures', 'Gateio', 'v4'): 'gateio',
      ('Futures', 'Gateio', 'v4-delivery'): 'gateio',
      ('Futures', 'Huobi', 'v1'): 'Huobi',
      ('Futures', 'Huobi', 'v1-swap'): 'Huobi',
      ('Futures', 'Huobi', 'v1-linear-swap'): 'Huobi',
      ('Futures', 'Kraken', 'v1'): 'kraken_futures',
      ('Futures', 'Kucoin', 'v1'): 'kucoin_fut',
      ('Futures', 'Okex', 'v5'): 'OKEx',
      ('Futures', 'Okex', 'v5-swap'): 'OKEx',
      ('Futures', 'Okex', 'v3'): 'OKEx',
      ('Futures', 'Okex', 'v3-swap'): 'OKEx',
      ('Futures', 'Okex', 'v3_swap'): 'OKEx',
      ('Futures', 'Phemex', 'v1'): 'phemex_futures',
      ('Futures', 'Phemex', 'v1-linear'): 'phemex_futures',
      ('Futures', 'Prex', 'v1'): 'prex',
      ('Margin', 'Binance', 'v1'): 'binance',
      ('Margin', 'Binance', 'snapshot'): 'binance',
      ('Margin', 'Bitfinex', "v2"): 'bitfinex_v2',
      ('Margin', 'Huobi', 'v1'): 'Huobi',
      ('Margin', 'Okex', 'v3'): 'OKEx',
      ('Options', 'Deribit', 'v2'): 'deribit_options',
      ('Spot', 'Binance', 'v1'): 'binance',
      ('Spot', 'Bitbank', 'v1'): 'bitbank',
      ('Spot', 'Bitfinex', "v2"): 'bitfinex_v2',
      ('Spot', 'Bithumb', 'v2'): 'bithumb',
      ('Spot', 'Bitmart', 'v3'): 'bitmart',
      ('Spot', 'Bitstamp', 'v2'): 'bitstamp',
      ('Spot', 'Btcturk', 'v1'): 'btcturk',
      ('Spot', 'Bybit', 'v3'): 'bybit',
      ('Spot', 'Coincheck', 'v1'): 'coincheck',
      ('Spot', 'Coinone', 'v2'): 'coinone',
      ('Spot', 'Crypto', 'v2'): 'crypto',
      ('Spot', 'Ftx', 'v1'): 'FTX',
      ('Spot', 'Gateio', 'v4'): 'gateio',
      ('Spot', 'Gdax', 'v1'): 'coinbasepro',
      ('Spot', 'Gopax', 'v1'): 'gopax',
      ('Spot', 'Huobi', 'v1'): 'Huobi',
      ('Spot', 'Korbit', 'v1'): 'korbit',
      ('Spot', 'Kraken', 'v1'): 'kraken',
      ('Spot', 'Kucoin', 'v1'): 'kucoin',
      ('Spot', 'Lbank', 'v2'): 'lbank',
      ('Spot', 'Lmaxdigital', 'v1'): 'lmax_digital',
      ('Spot', 'Maicoin', 'v2'): 'maicoin',
      ('Spot', 'Mercado', 'v4'): 'mercadobtc',
      ('Spot', 'Mexc', 'v2'): 'mexc',
      ('Spot', 'Mexc', 'v3'): 'mexc',
      ('Spot', 'Okex', 'v3'): 'OKEx',
      ('Spot', 'Okex', 'v5'): 'OKEx',
      ('Spot', 'Phemex', 'v1'): 'phemex',
      ('Spot', 'Prex', 'v1'): 'prex',
      ('Spot', 'Uniswap', 'v3'): 'uniswap',
      ('Spot', 'Uniswap', 'v3-arbitrum'): 'uniswap_arbitrum',
      ('Spot', 'Upbit', 'v1'): 'upbit',
      ('Spot', 'Upbitid', 'v1'): 'upbitid',
  }
  key_parent_dir = exchange_api_to_dir_map[(market_type, exchange, api_version)]
  key_dir = "%s/%s/%s" % (key_root, owner, key_parent_dir)
  key_file = os.path.join(key_dir, key_name)
  return key_file


def get_key_file(market_type, exchange, owner, api_version, key_type, check_exist=True, key_root=None):
  assert key_type in ('view', 'trade'), key_type
  key_root = key_root if key_root is not None else FLAGS.key_root
  if FLAGS.key_name is not None:
    key_name = FLAGS.key_name
  else:
    if exchange == 'Okex' and api_version is not None:
      if 'v3' in api_version:
        key_name = f'{key_type}_v3_key.json'
      elif 'v5' in api_version:
        key_name = f'{key_type}_v5_key.json'
    elif market_type == 'Futures' and exchange in ('Binance', 'Prex'):
      key_name = f'fut_{key_type}_key.json'
    else:
      key_name = f'{key_type}_key.json'
  key_file = os.path.expanduser(get_key_file_impl(
      key_root, key_name, market_type, exchange, owner, api_version))
  if check_exist:
    if not os.path.isfile(key_file):
      raise FileNotFoundError('Keyfile not found for (%s, %s, %s)' %
                              (exchange, api_version, key_file))
  return key_file
